package com.haruhi.ws;

import javax.websocket.CloseReason;
import javax.websocket.Endpoint;
import javax.websocket.EndpointConfig;
import javax.websocket.MessageHandler;
import javax.websocket.Session;

public class EndpointAdapter extends Endpoint {

    public void setListener(WsListener listener) {
        this.listener = listener;
    }

    private WsListener listener;

    public EndpointAdapter(WsListener listener){
        this.listener = listener;
    }
    public EndpointAdapter(){
    }
    @Override
    public void onOpen(Session session, EndpointConfig endpointConfig) {
        session.addMessageHandler(new MessageHandler.Whole<String>() {
            @Override
            public void onMessage(String s) {
                listener.onMessage(session,s);
            }
        });
        listener.onOpen(session,endpointConfig);
    }

    @Override
    public void onClose(Session session, CloseReason closeReason) {
        listener.onClose(session, closeReason);
    }

    @Override
    public void onError(Session session, Throwable throwable) {
        listener.onError(session, throwable);
    }
}
